package org.example.service;

import org.example.entity.Product;
import org.example.entity.Shanpin;
import org.example.mapper.Shanpinmapper;
import org.example.mapper.Wdzjmapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class WdzjService {
    @Autowired
    Shanpinmapper shanpinmapper;

    @Autowired
    Wdzjmapper wdzjmapper;
    /**
     * 我的足迹修改
     * @param id
     */
    public void xg(int id, String username) {
        LocalDateTime time=LocalDateTime.now();
        if(wdzjmapper.cx(id,username)==null){
            wdzjmapper.xz(id,time,username);
        }else{
            wdzjmapper.xg(id,time,username);
        }

    }

    /**
     * 足迹商品查询
     * @return
     */
    public List<Shanpin> shanpincx(String username) {

        List<Product> productList=shanpinmapper.productList();
        List<Shanpin> shanpinList=new ArrayList<>();
        for(Product p:productList){
            Object time=wdzjmapper.cx(p.getShanpinId(),username);
            if(time!=null) {
                Shanpin shanpin = shanpinmapper.cxid(p.getShanpinId());
                shanpin.setWdzjTime((LocalDateTime)time);
                shanpin.setYingfe(p.getTemplate());
                shanpinList.add(shanpin);
            }
        }
        return  shanpinList;
    }

    /**
     * 足迹删除
     */
    public void sc(int id, String username) {
        wdzjmapper.sc(id,username);
    }


    public void scqb(String username) {
        wdzjmapper.scqb(username);
    }
}
